package com.scs.application.modules.upms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.upms.entity.GenTable;
import com.scs.application.modules.upms.entity.GenTableColumn;
import com.scs.application.modules.upms.request.GenTableQueryRequest;
import com.scs.application.modules.upms.service.GenTableService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 */
@RestController
@RequestMapping("/upms/gen_tables")
public class GenTableController extends CrudController<GenTable, String, GenTableService> implements ImAndExAbleController<GenTableQueryRequest> {


    @GetMapping("/page")
    public PageResult page(GenTableQueryRequest request) {
        IPage page = service.page(request.getPage(), new QueryWrapper<GenTable>()
                .like(StringUtils.isNotBlank(request.getTableName()), "table_name", request.getTableName())
                .orderByDesc("gmt_modified")
        );

        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(GenTableQueryRequest request) {
        List<GenTable> list = service.list(new QueryWrapper<GenTable>()
                .like(StringUtils.isNotBlank(request.getTableName()), "table_name", request.getTableName()));
        return CommonResult.ok("查询成功", list);
    }


    /**
     * 从指定数据源中加载表信息
     *
     * @param datasourceId
     * @return
     */
    @GetMapping("/list_table_from_db")
    public CommonResult findTableListFromDB(@RequestParam("datasourceId") String datasourceId) {
        List<GenTable> list = service.findTableListFromDB(datasourceId);
        return CommonResult.ok("查询成功", list);
    }

    @GetMapping("/list_table_column_from_db")
    public CommonResult findTableListFromDB(@RequestParam("datasourceId") String datasourceId, @RequestParam("tableName") String tableName) {
        List<GenTableColumn> list = service.findTableColumnListFromDB(datasourceId, tableName);
        return CommonResult.ok("查询成功", list);
    }


    @PostMapping("/save_gen_info")
    public CommonResult<GenTable> saveOrUpdateGenInfo(@RequestBody @Valid GenTable genTable) {


        List<GenTableColumn> columns = genTable.getColumns();
        if (null == columns || columns.isEmpty()) {
            return CommonResult.error("保存失败，未配置字段信息");
        }

        service.saveOrUpdateGenInfo(genTable);

        return CommonResult.ok("保存成功", genTable);
    }

    @PostMapping("/save_and_generate")
    public CommonResult<List<String>> saveAndGenerate(@RequestBody @Valid GenTable genTable) {

        List<GenTableColumn> columns = genTable.getColumns();
        if (null == columns || columns.isEmpty()) {
            return CommonResult.error("保存失败，未配置字段信息");
        }


        return CommonResult.ok("保存并生成代码成功", service.saveAndGenerate(genTable));
    }


}
